( function() {

/////////////////////////////////////////////////////////////////

if(window.__njf__) return;
window.__njf__ = { animators:{}, animator_delay: 20 };
window.PLATFORM = /a/[-1]=='a'?'GECKO':'\v'=='v'?'IE':/a/.__proto__=='//'?
	'KHTML':/s/.test(/a/.toString)?'CHROME':/^function \(/.test([].sort)?
	'OPERA':'UNKNOWN';

var regx_trim = /^[\r\n\s\t]*|[\r\n\s\t]*$/g, regx_host = /^https?:\/{2}([:\[\]\-\w\.]+)\/?.*/ ,
    regx_tmpl = /[\s\t]*id=[\s\t]*([\"\']?)[\s\t]*([^\"\'\s\t:]*)[\s\t]*:[\s\t]*([^\"\'\s\t>:]+)[\s\t]*\1/gi;

var string    = String.prototype;
string.trim   = function()   { return this.replace(regx_trim, ''); };
window.trim   = function(s)  { return s.replace(regx_trim   , ''); };
window.$      = function(id) { return convert2widget(document.getElementById(id)); };
window.$form  = function(id) { return form($(id)); };
window.$body  = function()   { return convert2widget(document.body); };
window.$tmpl  = function(id) { return template($(id).innerHTML); };
window.new_id = function()   { return '_' + Math.ceil(Math.random() * 16777216) + '_' + (new Date()).getTime(); };

window.uriform = function() {
	var s = document.location.href, p = s.lastIndexOf('?');
	if(!p) return {};
	var l = s.substring(p + 1).split('&'), d = {};
	for(var i = 0; i < l.length; i++) {
		var t = l[i].split('=');
		if(t.length != 2)
			continue;

		var k = decodeURIComponent(t[0]), v = decodeURIComponent(t[1]);
		if(d.hasOwnProperty(k)) {
			if(typeof(d[k]) == 'string')
				d[k] = [d[k], v];
			else
				d[k][d[k].length] = v;
		} else
			d[k] = v;
	}
	return d;
};

window.obj2qstr = function(obj) {
	var qstr = [];
	for(var i in obj) {
		var s = obj[i];
		if(typeof(s) == 'string')
			qstr[qstr.length] = encodeURIComponent(i) + '=' + encodeURIComponent(s);
		else {
			var name = encodeURIComponent(i);
			for(var j = 0; j < s.length; j++)
				qstr[qstr.length] = name + '=' + encodeURIComponent(s[j]);
		}
	}
	return qstr.join("&");
};

window.modal = function(obj, color, opacity) {
	var bg1 = $body().append(), bg2 = $body().append(), ce = bg2.append(), bd = ce.append(),
	    bg_css = 'position:fixed;left:0;top:0;height:100%;left:0;right:0;';

	bg2.css(bg_css);
	bg1.css(bg_css);
	ce.css('display:table;margin:auto;height:100%;width:100%;');
	bd.css('display:table-cell;vertical-align:middle;');
	obj.style.position = 'absolute';
	obj.style.width    = obj.offsetWidth +'px';
	obj.style.position = '';

	bd.append(obj);

	obj.css('margin:auto;');
	obj.__del__ = obj.__del__ || {};
	obj.__del__['modal'] = function() {
		try { bg1.destroy(); } catch(e) {}
		try { bg2.destroy(); } catch(e) {}
	};
	obj.set_modal_bg = function(color, opacity) {
		color = color || '#fff';
		opacity = opacity || 0;
		bg1.css('background-color:'+color+';filter:alpha(opacity='+opacity*100+
			');-moz-opacity:'+opacity+';opacity:'+opacity);
	};
	obj.set_modal_bg(color, opacity);
	return obj;
};

if(PLATFORM == 'IE') {
	if(parseFloat(navigator.appVersion.split('MSIE')[1])<7) window.modal = function(obj, color, opacity) {
		var bg1 = $body().append('iframe'), bg2 = $body().append();
		bg1.css('position:absolute;filter:alpha(opacity=0);');
		bg2.style.position = 'absolute';
		var body = $body(), c1 = bg1.style, c2 = bg2.style,
		    store = function() {
			var scl = body.scrollLeft , sct = body.scrollTop,
			    clw = body.clientWidth, clh = body.clientHeight;

			c1.top    = c2.top    = sct;
			c1.left   = c2.left   = scl;
			c1.width  = c2.width  = clw;
			c1.height = c2.height = clh;

			var x = Math.ceil(scl+(clw-obj.offsetWidth )/2),
			    y = Math.ceil(sct+(clh-obj.offsetHeight)/2-40);

			obj.move((x>0?x:0), (y>0?y:0));

		},  h1 = window.attachEvent('onresize', store),
		    h2 = window.attachEvent('onscroll', store),
		    unstore = function() {
			window.detachEvent('onresize', store);
			window.detachEvent('onscroll', store);
			try { bg1.destroy(); } catch(e) {}
			try { bg2.destroy(); } catch(e) {}
		};

		obj.leave();
		obj.__del__ = obj.__del__||{};
		obj.__del__['modal'] = unstore;
		obj.set_modal_bg = function(color, opacity) {
			color = color || '#fff';
			opacity = opacity || 0;
			bg2.css('background-color:'+color+';filter:alpha(opacity='+opacity*100+');');
		};
		obj.set_modal_bg(color, opacity);
		store();
		return obj;
	};

	window.HttpRequest = function() { return new ActiveXObject('MSXML2.XMLHTTP'); };
	window.comet = function(uri, callback) {
		var hfile = new ActiveXObject('htmlfile');
		hfile.write('<iframe name="__njf_iframe__" src="about:blank"></iframe>') || hfile.close();
		hfile.parentWindow.__comet__ = window;
		if(callback) hfile.body.onload = function() { callback(hfile.__njf_iframe__); };
		hfile.__njf_iframe__.location.href = uri;
		return hfile;
	};
} else {
	if(!HTMLElement) { alert('ERROR:UNSUPPORT BROWSER'); return; }
	var element = HTMLElement.prototype;
	var ae = function(name, handler) {
		var n = name.replace(/^on/, '');
		this.addEventListener(n, handler, false);
	}; element.attachEvent  = ae;
	   document.attachEvent = ae;
	   window.attachEvent   = ae;

	var de = function (name, handler) {
		var n = name.replace(/^on/, '');
		this.removeEventListener(n, handler, false);
	}; element.detachEvent  = de;
	   document.detachEvent = de;
	   window.detachEvent   = de;

	window.HttpRequest = function() { return new XMLHttpRequest(); };
	if(PLATFORM == 'CHROME') window.comet = function(uri, callback) {
		var f1 = $body().append('iframe');
		f1.style.display = 'none';
		if(callback) f1.attachEvent('onload', callback);
		f1.src = uri;
		return f1;
	}; else window.comet = function(uri, callback) {
		var f1 = $body().append('iframe'), f2 = f1.append('iframe');
		f1.style.display = 'none';
		if(callback) f2.attachEvent('onload', callback);
		f2.contentDocument.location.href = uri;
		return f1;
	};
}

window.get = function(uri, o16e) {
	var req = HttpRequest();
	req.onreadystatechange = function() { o16e(req); };
	req.open('GET', uri, true);
	req.setRequestHeader('Host', uri.replace(regx_host, '$1'));
	req.send(null);
};

window.post = function(uri, data, o16e) {
	var req = HttpRequest();
	req.onreadystatechange = function() { o16e(req); };
	req.open('POST', uri, true);
	req.setRequestHeader('Host', uri.replace(regx_host, '$1'));
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.setRequestHeader('Content-Length', '' + data.length);
	req.send(typeof(data)=='string'?data:obj2qstr(data));
};

window.enterpress = function(obj, onenterpress) {
	obj.attachEvent('onkeypress', function(e) {
		if(e.keyCode == 13) onenterpress(obj);
	} );
};

window.convert2widget = function(obj) {
	if(obj.__njf_wseed__) return obj;
	var doc = obj.ownerDocument;
	obj.get_parent = function() { return convert2widget(obj.parentNode); };
	obj.copy = function() { return convert2widget(obj.cloneNode(1)); };
	obj.show = function(display) { obj.style.display = display||''; };
	obj.hide = function() { obj.style.display = 'none'; };

	obj.config = function() {
		var o = obj;
		for(var i = 0; i < arguments.length; i++)
			o = arguments[i](o);
		return o;
	};

	obj.css = function(sheet) {
		var l = sheet.split(';');
		for(var i = 0; i < l.length; i++) {
			var ll = l[i].split(':');
			if(ll.length != 2)
				continue;

			var c1 = ll[0].toLowerCase().trim();
			if(c1.search('-') == -1) {
				obj.style[c1] = ll[1].trim();
				continue;
			}

			var l4 = c1.split('-'), name = l4[0].trim();
			for(var k = 1; k < l4.length; k++) {
				var t = l4[k].trim();
				if(!t) continue;
				name += t.substring(0, 1).toUpperCase() + t.substring(1);
			}
			obj.style[name] = ll[1].trim();
		}
	};

	obj.append = function(tag, attrs) {
		tag = tag || 'DIV';
		if(typeof(tag) == 'string') {
			var node = doc.createElement(tag);
			if(attrs) for(var an in attrs)
				node.setAttribute(an, attrs[an]);

			obj.appendChild(node);
			return convert2widget(node);
		}
		var node = doc.createElement('DIV');
		obj.appendChild(node);
		obj.replaceChild(tag, node);
		return convert2widget(tag);
	};

	obj.prepend = function(tag, attrs) {
		tag = tag || 'DIV';
		if(typeof(tag) == 'string') {
			var node = doc.createElement(tag);
			if(attrs) for(var an in attrs)
				node.setAttribute(an, attrs[an]);

			obj.insertBefore(node, obj.firstChild);
			return convert2widget(node);
		}
		var node = doc.createElement('DIV');
		obj.insertBefore(node, obj.firstChild);
		obj.replaceChild(tag, node);
		return convert2widget(tag);
	};

	obj.previous = function(tag, attrs) {
		tag = tag || 'DIV';
		if(typeof(tag) == 'string') {
			node = doc.createElement(tag);
			if(attrs) for(var an in attrs)
				node.setAttribute(an, attrs[an]);

			obj.parentNode.insertBefore(node, obj);
			return convert2widget(node);
		}
		var node = doc.createElement('DIV');
		obj.parentNode.insertBefore(node, obj);
		obj.parentNode.replaceChild(tag, node);
		return convert2widget(tag);
	};

	obj.next = function(tag, attrs) {
		tag = tag || 'DIV';
		var node;
		if(typeof(tag) == 'string') {
			var node = doc.createElement(tag);
			if(attrs) for(var an in attrs)
				node.setAttribute(an, attrs[an]);

			obj.parentNode.insertBefore(node, obj.nextSibling);
			return convert2widget(node);
		}

		var node = doc.createElement('DIV');
		obj.parentNode.insertBefore(node, obj.nextSibling);
		obj.parentNode.replaceChild(tag, node);
		return convert2widget(tag);
	};

	obj.replace_by = function(node) {
		obj.parentNode.replaceChild(node, obj);
		return convert2widget(node);
	};

	obj.destroy = function() {
		if(obj.__del__) {
			for(var i in obj.__del__)
				obj.__del__[i]();

			obj.__del__ = undefined;
		}
		obj.parentNode.removeChild(obj);
		return obj;
	};

	obj.leave = function() {
		var r = obj.style;
		if(arguments.length == 2) {
			r.left = arguments[0];
			r.top = arguments[1];
		} else {
			r.left = obj.offsetLeft;
			r.top = obj.offsetTop;
		}
		r.position = 'absolute';
		$body().append().replace_by(obj);
		return obj;
	};

	obj.move = function(x, y) {
		var r = obj.style;
		r.left = x;
		r.top  = y;
		return obj;
	};

	obj.stay = function(pnt) {
		obj.style.position = 'relative';
		if(pnt) pnt.append(obj);
		return obj;
	};

	if(obj.tagName == 'SELECT') {
		obj.option = function(v, s) {
			var op = this.append('OPTION');
			op.value = v;
			if(s) op.innerHTML = s;
			return op;

		}; obj.new_option = obj.option;

		obj.clear = function() {
			var l = this.getElementsByTagName('OPTION');
			while(l.length) for(var i = 0; i < l.length; i++)
				this.removeChild(l[i]);
		};
	}
	obj.__njf_wseed__ = true;
	return obj;
};

window.template = function(s) {
	var m = regx_tmpl.exec(s), l0 = [], l1 = [], d = {}, obj = $body().append();
	while(m) {
		l0[l0.length] = m[0];
		l1[l1.length] = m[3];
		m = regx_tmpl.exec(s);
	}
	for(var i = 0; i < l0.length; i++) {
		var uid = new_id();
		d[l1[i]] = uid;
	}
	obj.innerHTML = s.replace(regx_tmpl, function() { return ' id="' + d[arguments[3]] + '" '; } );
	for(var i = 0; i < l1.length; i++) {
		obj[l1[i]] = $(d[l1[i]]);
	}
	return obj;

};

window.form = function(obj) {
	if(obj.__njf_formed__) return obj;
	var obj = obj || $body().append(), checkable_types = { CHECKBOX: null, RADIO: null }, setdisabled =
	function(obj, ll, disabled) {
		var p = ['INPUT', 'TEXTAREA', 'SELECT'], d = {};
		if(ll) for(var i = 0; i < ll.length; i++) d[ll[i]] = null;
		for(var i = 0; i < p.length; i++) {
			var l = obj.getElementsByTagName(p[i]);
			for(var j = 0; j < l.length; j++) {
				var item = l[j];
				if(ll && !d.hasOwnProperty(item.name)) continue;
				item.disabled = disabled;
			}
		}
	}; obj.enable  = function() {
		setdisabled(obj,
			(arguments.length?
				(arguments.length == 1 && typeof(arguments[0]) != 'string'?
					arguments[0] : arguments) : null), false);
	}; obj.disable = function() {
		setdisabled(obj,
			(arguments.length?
				(arguments.length == 1 && typeof(arguments[0]) != 'string'?
					arguments[0] : arguments) : null), true ); };
	var setcontent = function(d, k, v) {
		if(d.hasOwnProperty(k)) {
			if(typeof(d[k]) == 'string') d[k] = [d[k], v];
			else d[k][d[k].length] = v;
		} else d[k] = v;

	};

	obj.get = function() {
		var d = {}, l = obj.getElementsByTagName('TEXTAREA');
		for(var i = 0; i < l.length; i++)
			setcontent(d, l[i].name, l[i].value);

		l = obj.getElementsByTagName('SELECT');
		for(var i = 0; i < l.length; i++)
			setcontent(d, l[i].name, l[i].value);

		l = obj.getElementsByTagName('INPUT');
		for(var i = 0; i < l.length; i++) {
			var item = l[i];
			if(checkable_types.hasOwnProperty(item.type.toUpperCase())) {
				if(item.checked) {
					if(d.hasOwnProperty(item.name)) {
						var t = d[item.name];
						t[t.length] = item.value;
					} else
						d[item.name] = [item.value];

				} else if(!d.hasOwnProperty(item.name))
					d[item.name] = [];

			} else d[item.name] = item.value;
		}
		return d;

	};

	obj.set = function(d) {
		var l = obj.getElementsByTagName('TEXTAREA');
		for(var i = 0; i < l.length; i++) {
			var item = l[i]; if(d.hasOwnProperty(item.name))
				item.value = d[item.name];

		};

		l = obj.getElementsByTagName('SELECT');
		for(var i = 0; i < l.length; i++)  {
			var item = l[i]; if(d.hasOwnProperty(item.name))
				item.value = d[item.name];

		};

		l = obj.getElementsByTagName('INPUT');
		for(var i = 0; i < l.length; i++) {
			var item = l[i];
			if(d.hasOwnProperty(item.name)) {
				if(checkable_types.hasOwnProperty(item.type.toUpperCase())) {
					var v = item.value, ll = d[item.name], checked = false;
					for(var j = 0; j < ll.length; j++)
						if(ll[j] == v) {
							checked = true;
							break;
						}
					item.checked = checked;

				} else item.value = d[item.name];
			}
		}
	};

	obj.qstr = function(l) {
		var f = true, d = obj.get(), qstr = [], p = {};
		if(l) for(var i = 0; i < l.length; i++) {
			var name = l[i];
			if(!d.hasOwnProperty(name)) d[name] = '';
			p[name] = null;
		} else
			f = false;

		for(var i in d) {
			if(f && !p.hasOwnProperty(i)) continue;
			var s = d[i];
			if(typeof(s) == 'string')
				qstr[qstr.length] = encodeURIComponent(i) + '=' + encodeURIComponent(s);
			else {
				var name = encodeURIComponent(i);
				for(var j = 0; j < s.length; j++)
					qstr[qstr.length] = name + '=' + encodeURIComponent(s[j]);
			}
		}
		return qstr.join('&');

	};

	obj.ajax_submit = function(o16e) {
		var method = obj.method || 'POST', action = obj.action || '', data = obj.qstr();
		if(data) {
			if(method.toUpperCase()=='GET')
				get(action+'?'+data, o16e);
			else
				post(action, data, o16e);
		} else
			get(action, o16e);
	};

	obj.__njf_formed__ = true;
	return obj;

};

window.dragaware = function(begin, handler) {
	var obj = {}, move  = function(e) {
		handler((e.clientX<1?1:e.clientX) - obj.x1,
		        (e.clientY<1?1:e.clientY) - obj.y1);

	}, stop  = function(e) {
		document.detachEvent('onmouseup'  , stop);
		document.detachEvent('onmousemove', move);

	}, start = function(e) {
		begin();

		if(PLATFORM != 'IE') e.preventDefault();
		obj.y1 = e.clientY;
		obj.x1 = e.clientX;
		document.attachEvent('onmouseup'  , stop);
		document.attachEvent('onmousemove', move);
	};

	obj.enable  = function(src) { src.attachEvent('onmousedown', start); };
	obj.disable = function(src) { src.detachEvent('onmousedown', start); };
	return obj;
};

window.drag = function(src, dest) {
	if(!dest) {
		var cp = src;
		while(cp.parentNode && cp.parentNode != document.body)
			cp = cp.parentNode;

		dest = convert2widget(cp);
	}

	var xinit = dest.offsetLeft, yinit = dest.offsetTop,
	    skip = function() { return false; },
	    obj  = dragaware(function() {
		obj.yinit = dest.offsetTop;
		obj.xinit = dest.offsetLeft;

	}, function(x, y) {
		var x = obj.xinit+x*obj.x_speed, y = obj.yinit+y*obj.y_speed;
		if(('min_x' in dest)&&x<dest.min_x) x = dest.min_x;
		if(('max_x' in dest)&&x>dest.max_x) x = dest.max_x;
		if(('min_y' in dest)&&y<dest.min_y) y = dest.min_y;
		if(('max_y' in dest)&&y>dest.max_y) y = dest.max_y;
		dest.move(x, y);
	});

	obj.x_speed = obj.y_speed = 1;
	dest.style.position = dest.style.position || 'absolute';
	dest.move(xinit, yinit);
	src.css('-moz-user-focus: ignore; -moz-user-input: disabled; -moz-user-select: none; cursor: default;');
	src.attachEvent('onselect', skip);
	src.attachEvent('ondragstart', skip);
	src.attachEvent('onselectstart', skip);
	src.enable_drag  = function() { obj.enable(src);  };
	src.disable_drag = function() { obj.disable(src); };
	obj.enable(src);
	return obj;
};

window.resize = function(src, dest) {
	if(!dest) {
		var cp = src;
		while(cp.parentNode && cp.parentNode != document.body)
			cp = cp.parentNode;

		dest = convert2widget(cp);
	}

	var skip = function() { return false; }, obj = dragaware(function() {
		obj.winit = dest.clientWidth;
		obj.hinit = dest.clientHeight;

	}, function(x, y) {
		var w = obj.winit + x * obj.w_speed, h = obj.hinit + y * obj.h_speed;
		if(('min_w' in dest)&&w<dest.min_w) w = dest.min_w;
		if(('max_w' in dest)&&w>dest.max_w) w = dest.max_w;
		if(('min_h' in dest)&&h<dest.min_h) h = dest.min_h;
		if(('max_h' in dest)&&h>dest.max_h) h = dest.max_h;
		try { dest.style.width  = w+'px'; } catch(e) {}
		try { dest.style.height = h+'px'; } catch(e) {}
	});

	obj.w_speed = obj.h_speed = 1;
	src.css('-moz-user-focus: ignore; -moz-user-input: disabled; -moz-user-select: none; cursor: default;');
	src.attachEvent('onselect', skip);
	src.attachEvent('ondragstart', skip);
	src.attachEvent('onselectstart', skip);
	src.enable_resize  = function() { obj.enable(src);  };
	src.disable_resize = function() { obj.disable(src); };
	obj.enable(src);
	return obj;
};

window.animator = function(times, handler) {
	obj = { name:new_id(), times:times, counter:0, st:1, delay:__njf__.animator_delay };
	obj.step = function() {
		if(obj.st == 2) {
			if(obj.times == 0) {
				handler(obj.counter);
				setTimeout('__njf__.animators[\"' + obj.name + '\"].step()', obj.delay);
				return;
			}

			obj.counter++;
			if(obj.counter > obj.times) {
				delete __njf__.animators[obj.name];
				return;
			}

			handler(obj.counter);
			setTimeout('__njf__.animators[\"' + obj.name + '\"].step()', obj.delay);

		} else if(obj.st == 1) setTimeout('__njf__.animators[\"' + obj.name + '\"].step()', 100);
		  else delete __njf__.animators[obj.name];

	};
	obj.stop  = function() { obj.st = 0; };
	obj.pause = function() { obj.st = 1; };
	obj.start = function() { obj.st = 2; };
	__njf__.animators[obj.name] = obj;
	setTimeout('__njf__.animators[\"' + obj.name + '\"].step()', 1);
	return obj;
};

window.pos_animator = function(dest, x, y, n) {
	yinit = dest.offsetTop;
	xinit = dest.offsetLeft;
	obj = animator(n, function(t) {
		var x = xinit, y = yinit;
		dest.move(obj.x==null?dest.offsetLeft:eval(obj.x),
		          obj.y==null?dest.offsetTop :eval(obj.y));
	});

	obj.x = x=='keep'?null:x;
	obj.y = y=='keep'?null:y;
	dest.style.position = 'absolute';
	obj.start();
	return obj;
};

window.size_animator = function(dest, w, h, n) {
	winit = dest.clientWidth;
	hinit = dest.clientHeight;
	obj = animator(n, function(t) {
		var w = winit, h = hinit;
		dest.style.width  = obj.w==null?dest.clientWidth :eval(obj.w) + 'px';
		dest.style.height = obj.h==null?dest.clientHeight:eval(obj.h) + 'px';
	});

	obj.w = w=='keep'?null:w;
	obj.h = h=='keep'?null:h;
	obj.start();
	return obj;
};

window.dir = function(o) {
	var l = [];
	for(var i in o)
		l[l.length] = i;

	l = l.sort();
	for(var i = 0; i < l.length; i++)
		document.write(l[i].replace('<', '&lt;').replace('>', '&gt;') + '<br />');
};

/////////////////////////////////////////////////////////////////

} )();
